package com.jtny.nytb.datafilling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jtny.component.core.util.PageUtils;
import com.jtny.component.entity.PageData;
import com.jtny.component.entity.PageParam;
import com.jtny.component.exception.ServiceException;
import com.jtny.component.util.BeanUtils;
import com.jtny.nytb.datafilling.dao.DatafillingEnergypriceFillDAO;
import com.jtny.nytb.datafilling.dao.DatafillingUserelectricFillDAO;
import com.jtny.nytb.datafilling.domain.DatafillingUserelectircFillDO;
import com.jtny.nytb.datafilling.domain.DatafillingEnergypriceFillDO;
import com.jtny.nytb.datafilling.dto.DatafillingUserelectircFillDTO;
import com.jtny.nytb.datafilling.service.DatafillingUserElectircService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DatafillingUserElectircServiceImpl implements DatafillingUserElectircService {

    @Autowired
    private DatafillingUserelectricFillDAO datafillingUserelectricFillDAO;

    @Override
    public Boolean addUserElectricInfo(DatafillingUserelectircFillDTO datafillingUserelectircFillDTO) {
        String yearMonth = new SimpleDateFormat("yyyy-MM").format(new Date());
        DatafillingUserelectircFillDO datafillingUserelectircFillDO = BeanUtils.copyToObject(datafillingUserelectircFillDTO,DatafillingUserelectircFillDO.class);
        datafillingUserelectircFillDO.setFillTime(yearMonth);
        return datafillingUserelectricFillDAO.insert(datafillingUserelectircFillDO)==1;
    }

    @Override
    public Boolean saveUserElectricInfo(DatafillingUserelectircFillDTO datafillingUserelectircFillDTO) {
        String yearMonth = "";
        if (StringUtils.isEmpty(datafillingUserelectircFillDTO.getFillTime())){
            yearMonth = new SimpleDateFormat("yyyy-MM").format(new Date());
        }else {
            yearMonth = datafillingUserelectircFillDTO.getFillTime();
        }
        LambdaQueryWrapper<DatafillingUserelectircFillDO> queryWrapper = Wrappers.lambdaQuery(DatafillingUserelectircFillDO.class)
                .eq(DatafillingUserelectircFillDO::getId,datafillingUserelectircFillDTO.getId());
        DatafillingUserelectircFillDO datafillingUserelectircFillDO = datafillingUserelectricFillDAO.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(datafillingUserelectircFillDO)){
            datafillingUserelectircFillDO = BeanUtils.copyToObject(datafillingUserelectircFillDTO,DatafillingUserelectircFillDO.class);
            datafillingUserelectircFillDO.setFillTime(yearMonth);
            return datafillingUserelectricFillDAO.insert(datafillingUserelectircFillDO)==1;
        }else {
            DatafillingUserelectircFillDO update = BeanUtils.copyToObject(datafillingUserelectircFillDTO,DatafillingUserelectircFillDO.class);
            update.setId(datafillingUserelectircFillDO.getId());
            return datafillingUserelectricFillDAO.updateById(update)==1;
        }
    }

    @Override
    public PageData<DatafillingUserelectircFillDTO> queryPage(PageParam pageParam) throws ServiceException {
        Page page = Page.of(pageParam.getPageNo(), pageParam.getPageSize());
        QueryWrapper<DatafillingUserelectircFillDO> qw = new QueryWrapper<>();
        qw.orderByDesc("fill_time");
        PageData<DatafillingUserelectircFillDO> pageDataForDO = PageUtils.getPageData(datafillingUserelectricFillDAO.selectPage(page,qw), pageParam);
        return pageDataForDO.transformData(DatafillingUserelectircFillDTO.class);
    }

    @Autowired
    private DatafillingEnergypriceFillDAO datafillingEnergypriceFillDAO;

    @Override
    public List<String> queryPowerFactor() throws ServiceException {
        return  datafillingEnergypriceFillDAO.selectList(null).stream()
                .filter(d->!d.getSystemGrade().equals("0.80")&&!d.getSystemGrade().equals("0.85")&&!d.getSystemGrade().equals("0.90"))
                .map(DatafillingEnergypriceFillDO::getSystemGrade).collect(Collectors.toList());
    }
}
